// pages/zhuye_xinlidayi/zhuye_xinlidayi.js
import {
  request
} from "../../utils/http.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 1,
    list: [

    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    request({
      url: "/question/findAll"+this.data.index
    }).then((data) => {
      console.log(data.data);
      this.setData({
        list: data.data
      })

      let date = new Date();
      for (let i = 0; i < this.data.list.length; i++) {
        let date1 = new Date(this.data.list[i].time);
        const timeDifference = date - date1;
        const millisecondsInADay = 24 * 60 * 60 * 1000;
        const daysDifference = Math.floor(timeDifference / millisecondsInADay);
        const hoursDifference = Math.floor((timeDifference % millisecondsInADay) / (60 * 60 * 1000));
        const minutesDifference = Math.floor((timeDifference % (60 * 60 * 1000)) / (60 * 1000));
        const secondsDifference = Math.floor((timeDifference % (60 * 1000)) / 1000);
        if (daysDifference <= 5 && daysDifference > 0) {
          this.data.list[i].time = daysDifference + "天前"
        } else if (daysDifference > 5) {
          this.data.list[i].time = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();
        } else {
          if (hoursDifference == 0) {
            if (minutesDifference == 0) this.data.list[i].time = secondsDifference + "秒前";
            else this.data.list[i].time = minutesDifference + "分钟前";
          } else {
            this.data.list[i].time = hoursDifference + "小时前";
          }
        }
      }
      this.setData({
        list: this.data.list
      });
    }).catch((err) => {
      console.log(err);
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  fanhuei() {
    wx.navigateBack()
  },

  xuanze(e) {
    this.setData({
      index: e.currentTarget.id
    })
    request({
      url: "/question/findAll"+e.currentTarget.id
    }).then((data) => {
      console.log(data.data);
      this.setData({
        list: data.data
      })

      let date = new Date();
      for (let i = 0; i < this.data.list.length; i++) {
        let date1 = new Date(this.data.list[i].time);
        const timeDifference = date - date1;
        const millisecondsInADay = 24 * 60 * 60 * 1000;
        const daysDifference = Math.floor(timeDifference / millisecondsInADay);
        const hoursDifference = Math.floor((timeDifference % millisecondsInADay) / (60 * 60 * 1000));
        const minutesDifference = Math.floor((timeDifference % (60 * 60 * 1000)) / (60 * 1000));
        const secondsDifference = Math.floor((timeDifference % (60 * 1000)) / 1000);
        if (daysDifference <= 5 && daysDifference > 0) {
          this.data.list[i].time = daysDifference + "天前"
        } else if (daysDifference > 5) {
          this.data.list[i].time = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();
        } else {
          if (hoursDifference == 0) {
            if (minutesDifference == 0) this.data.list[i].time = secondsDifference + "秒前";
            else this.data.list[i].time = minutesDifference + "分钟前";
          } else {
            this.data.list[i].time = hoursDifference + "小时前";
          }
        }
      }
      this.setData({
        list: this.data.list
      });
    }).catch((err) => {
      console.log(err);
    })
  },

  to(e) {
    wx.navigateTo({
      url: "/pages/zhuye_xinlidayi_cakanhufu/zhuye_xinlidayi_cakanhufu?id=" + e.currentTarget.id
    })
  },
  toQuest(e){
    wx.navigateTo({
      url: e.currentTarget.id
    })
  }
})